<?php
if (!empty($dataPackage)){
    foreach ($dataPackage as $k => $v) {
        //----资产包----start---
        if ($v['_index'] == "package"){
            $package_title         = !empty($v['_source']['package_title'])?$v['_source']['package_title']:"";        //标题
            $package_creditor      = !empty($v['_source']['creditor'])?$v['_source']['creditor']:""; //持包人名称
            $package_code      = !empty($v['_source']['code'])?$v['_source']['code']:""; //资产包编号
            $package_principal      = !empty($v['_source']['principal'])?\common\helps\formatMoney::format_money($v['_source']['principal']):""; //资产包本金
            $package_interest      = !empty($v['_source']['interest'])?\common\helps\formatMoney::format_money($v['_source']['interest']):""; //资产包利息
            $package_hushu      = !empty($v['_source']['hushu'])?$v['_source']['hushu']:""; //资产包剩余户数
            $package_created_at = !empty($v['_source']['created_at'])?$v['_source']['created_at']:"";//上传时间
            $timeCreate = mb_substr($package_created_at,0,mb_stripos($package_created_at,'T'));
            $package_state      = !empty(Yii::$app->params['package_state'][$v['_source']['state']])?Yii::$app->params['package_state'][$v['_source']['state']]:"";//状态
            //--------联系人---start--
            $contact = $phone  = "";
            if (!empty($package_transfer)){
                foreach ($package_transfer as $k1 => $v1) {
                    if ($v1['_source']['package_id'] == $v["_id"]){
                        $contact = !empty($v1['_source']['contact'])?$v1['_source']['contact']:""; //联系人
                        $phone   = !empty($v1['_source']['phone'])?$v1['_source']['phone']:""; //电话',
                    }
                }
            }
            //--------联系人----end---
            //-----资产包负责人-----start--
            $leader = $owner = $description = "";
            if (!empty($package_initiation)){
                foreach ($package_initiation as $k1 => $v1) {
                    if ($v1['_source']['package_id'] == $v["_id"]){
                        $leaderId = !empty($v1['_source']['leader'])?str_replace(',', '', $v1['_source']['leader']):""; //资产包负责人ID
                    }
                }
                if (!empty($leaderId)&&!empty($user)){
                    foreach ($user as $k2 => $v2) {
                        if ($v2['_id'] == $leaderId){
                            $leader = !empty($v2['_source']['realname'])?$v2['_source']['realname']:""; //资产包负责人
                            if (!empty($auth_item)){
                                foreach ($auth_item as $k3 => $v3){
                                    if ($v3['_source']['name'] == $v2['_source']['department']){
                                        $description = !empty($v3['_source']['description'])?$v3['_source']['description']:""; //资产包负责人的部门
                                    }
                                }
                            }
                        }
                    }
                }
            }
            $leaderId = !empty($v['_source']['owner'])?$v['_source']['owner']:""; //资产包负责人
            if (!empty($leaderId)&&!empty($user)){
                foreach ($user as $k2 => $v2) {
                    if ($v2['_id'] == $leaderId){
                        $owner = !empty($v2['_source']['realname'])?$v2['_source']['realname']:""; //资产包负责人
                        if (!empty($auth_item)){
                            foreach ($auth_item as $k3 => $v3){
                                if ($v3['_source']['name'] == $v2['_source']['department']){
                                    $description = !empty($v3['_source']['description'])?$v3['_source']['description']:""; //资产包负责人的部门
                                }
                            }
                        }
                    }
                }
            }
            $leader = !empty($leader)?$leader:$owner;
            //-----资产包负责人-----end--
            //----- 尽调成员---start---
            $due_diligence = '';
            if (!empty($package_initiation)){
                foreach ($package_initiation as $k1 => $v1) {
                    if ($v1['_source']['package_id'] == $v["_id"]){
                        $due_diligence_id = !empty($v1['_source']['due_diligence'])?$v1['_source']['due_diligence']:""; //资产包负责人ID
                    }
                }
                if (!empty($due_diligence_id)&&!empty($user)){
                    $due_diligence_arr = explode(',',$due_diligence_id);
                    foreach ($due_diligence_arr as $k3 => $v3) {
                        foreach ($user as $k2 => $v2) {
                            if ($v2['_id'] == $v3){
                                $due_diligence.= !empty($v2['_source']['realname'])?$v2['_source']['realname'].',':""; //资产包负责人
                            }
                        }
                    }
                }
            }
            //-----尽调成员-----end--
            //---table-------start---
            //---权限
            $filterPackage = \backend\controllers\DtActionFilter::checkPackage("package_view",$v['_id']);
            $Redpackage_title = preg_replace($normalStr, $tagStr, $package_title);
            $packageId = $v['_id'];
            if ($filterPackage){
                $packageUrl =  <<<AURL
    <a href='/package/detail-info?packageid=$packageId' target='_blank'><span class='pull-left'>$Redpackage_title</span></a>
AURL;
               $packageFilterHtml = '';
            }else{
                $packageUrl =  <<<AURL
<a onclick="lmsgPack($packageId,'$package_title')"><span class='pull-left'>$Redpackage_title</span></a>
AURL;

                $packageFilterHtml = '<div class="pull-right permission">无访问权限</div>';
            }
            if (!empty($package_state)){
                $statesHtml = '<div class="pull-right project">'.$package_state.'</div>';
            }else{
                $statesHtml = '';
            }
            echo <<<TABLEPACAKGE
            <div class="searchLists clearfix searchLists-pakage">
                 <div class="searchLists-nav clearfix">
                                     $packageUrl
                    <div class="pull-left package">资产包</div>
                    $packageFilterHtml
                    $statesHtml
                </div>
                <div class="searchLists-content">
                    <span>持包人名称：<font>$package_creditor</font></span>
                    <span>本金：	<font>$package_principal</font></span>
                    <span>利息： <font>$package_interest</font></span>
                    <span>剩余户数： <font>$package_hushu 户</font></span></br>
                    <span>联系人： <font>$contact</font></span>
                    <span>电话： <font>$phone</font></span>
                    <span>资产包负责人： <font>$description - $leader</font></span>
                    <span>尽调成员： <font>$due_diligence</font></span>
                    
         		</div>
                <div class="sc">
                    <span class="pull-right">  $timeCreate $owner 上传</span>
                </div>
           </div>
TABLEPACAKGE;
            //---table-------end---
        }
        //----资产包----end---
    }
}
?>
<script>
    function lmsgPack(id,title) {
        var title = title;
        $("#packageId").val(id);
        $("#shenqingFW").modal();
        $("#title").val(title)
    }
    //------------------------------申请访问----fly------------------
    $(function () {
        $("#saveFWAccess").click(function () {
            var shenqingFWNote = $("#shenqingFWNote").val();
            var packageId = $("#packageId").val();
            var title = $("#title").val();

            var url = '/package-apply/access-apply'
            if (shenqingFWNote!=''){
                $.post(url,{'packageId':packageId,'shenqingFWNote':shenqingFWNote,'title':title,'group':1},function (res) {
                    resData = JSON.parse(res)
                    layer.msg(resData.message)
                    if (resData.code == 200){
                        $("#shenqingFW").modal('hide');
                    }
                })
            }else{
                layer.msg('请填写访问申请理由');
            }
        })
    })
    //------------------------------申请访问----fly------------------

</script>




